package com.whpost.service.zhdt.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import com.whpost.service.zhdt.dao.ZhdtWgxxKhjlMapper;
import com.whpost.service.zhdt.dao.ZhdtWgxxKhxxMapper;
import com.whpost.service.zhdt.entity.ZhdtWgxx;
import com.whpost.service.zhdt.entity.ZhdtWgxxKhjl;
import com.whpost.service.zhdt.entity.ZhdtWgxxKhxx;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ZhdtWgxxKhjlServices extends BaseServices<ZhdtWgxxKhjlMapper, ZhdtWgxxKhjl> {
    @Autowired
    private ZhdtWgxxServices wgxxServices;

    @Override
    public ResultInfo save(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String lx="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            ZhdtWgxxKhjl manager = jsonObject.toJavaObject(ZhdtWgxxKhjl.class);
            if (StringUtils.isEmpty(manager.getName())){
                return new ResultInfo(500,"客户名称不能为空！",null);
            }
            TCzy tCzy = gettCzyDao().selectById(loginId);
            manager.setCzyId(tCzy.getId());
            manager.setCzjgId(tCzy.getXzjgId());
            manager.setCreateDate(new Date());
            ResultInfo resultInfo;
            if ("1".equals(lx)){
                ZhdtWgxxKhjl oldManager = getBaseMapper().selectById(manager.getId());
                if (!String.valueOf(oldManager.getCzyId()).equals(loginId) && !"1".equals(loginId)){
                    return new ResultInfo(500,"非自己维护的网格信息不得修改！",null);
                }
                getBaseMapper().updateById(manager);
                resultInfo = new ResultInfo(2, "修改成功！",null);
            }else{
                manager.setState("1");
                getBaseMapper().insert(manager);
                resultInfo = new ResultInfo(1, "新增成功！",manager);
            }
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo info(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            TCzy tCzy = gettCzyDao().selectById(loginId);

            Map<String,Object> infoMap = new HashMap<>();

            String id="";
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            String lx="1";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
                if(StrUtil.isEmpty(lx)) lx="1";
            }

            if ("1".equals(lx)){
                ZhdtWgxxKhjl wgxxKhjl = getBaseMapper().selectById(id);
                infoMap.put("khjl",wgxxKhjl);
            }

            //除了admin，只能选本机构的
            List<ZhdtWgxx> wgxxList = new ArrayList<>();
            if("1".equals(loginId)){
                wgxxList = wgxxServices.lambdaQuery().eq(ZhdtWgxx::getState,"1").list();
            }else{
                wgxxList = wgxxServices.lambdaQuery().eq(ZhdtWgxx::getState,"1").eq(ZhdtWgxx::getXzjgId,tCzy.getXzjgId()).list();
            }
            infoMap.put("wgxxList",wgxxList);
            //给出默认值
            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo query(String loginId, Map<String, Object> map) {
        try{
            JSONObject jsonObject = getData(map,loginId);
            if (StringUtils.isEmpty(loginId)){
                return new ResultInfo(500,"用户信息丢失！",null);
            }else{
                String curpage = "1",pagesize="10",name="",state="",dlmc="";
                QueryWrapper<ZhdtWgxxKhxx> queryWrapper = new QueryWrapper<>();
                if (jsonObject.containsKey("curpage")) {
                    curpage = jsonObject.getString("curpage");
                    if (StringUtils.isEmpty(curpage)) curpage = "1";
                }
                if (jsonObject.containsKey("pagesize")) {
                    pagesize = jsonObject.getString("pagesize");
                    if (StringUtils.isEmpty(pagesize)) pagesize = "10";
                }
                if (jsonObject.containsKey("name")) {
                    name = jsonObject.getString("name");
                    if (!StringUtils.isEmpty(name))
                    {
                        queryWrapper.like("name",name);
                    }
                }
                if (jsonObject.containsKey("state")) {
                    state = jsonObject.getString("state");
                    if (!StringUtils.isEmpty(state))
                    {
                        queryWrapper.eq("state",state);
                    }
                }

                Page<ZhdtWgxxKhjl> page = new Page<>( Integer.parseInt(curpage),  Integer.parseInt(pagesize));
                ListInfo<ZhdtWgxxKhjl> listInfo = new ListInfo<>();
                queryWrapper.orderByDesc("id");
                IPage<ZhdtWgxxKhjl> pages = getBaseMapper().getZhtdWgxxKhjlPage(page,queryWrapper);
                pages.getRecords().forEach(khxx ->{//手机号脱敏
                    if(StrUtil.isNotEmpty(khxx.getPhone())){
                        if(khxx.getPhone().length()==11){
                            khxx.setPhone(StrUtil.hide(khxx.getPhone(),3,7));
                        }
                    }
                });
                PageInfo pageInfo = new PageInfo();
                pageInfo.setTotal(pages.getTotal());
                pageInfo.setCurPage(pages.getCurrent());
                pageInfo.setPageSize(pages.getSize());
                pageInfo.setPageTotal(pages.getPages());

                listInfo.setPageInfo(pageInfo);
                listInfo.setListInfo(pages.getRecords());
                listInfo.setUserId(loginId);

                //查询条件的值
                Map<String,Object> cxtjMap = new HashMap<>();
                cxtjMap.put("name",name);
                cxtjMap.put("state",state);

                //返回前端的内容
                Map<String,Object> rtnMap = new HashMap<>();
                rtnMap.put("cxjg",listInfo);
                rtnMap.put("cxtj",cxtjMap);

                ResultInfo resultInfo = new ResultInfo(0, "成功返回！",rtnMap);
                return backRtn(resultInfo,loginId);
            }
        }catch (Exception e){
            e.printStackTrace();
            ResultInfo resultInfo = new ResultInfo(500, "系统发生错误！",null);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    @Transactional
    public ResultInfo del(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String ids = "";
            if (jsonObject.containsKey("ids")) {
                ids = jsonObject.getString("ids");
            }
            String [] code = ids.split(",");
//            TCzy tCzy = gettCzyDao().selectById(loginId);
            this.lambdaUpdate()
                    .set(ZhdtWgxxKhjl::getState,"0")
                    .in(ZhdtWgxxKhjl::getId,code)
                    .eq(ZhdtWgxxKhjl::getCzyId,loginId)
                    .update();
            return new ResultInfo(3,"删除成功！",null);
        }
    }
}
